package com.yiren.manager.ctrl.pension;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.utils.FileUtil;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.PropertyConfigurer;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrMaterial;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.pension.YrMaterialForm;
import com.yiren.manager.service.iface.pension.YrMaterialService;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("material")
@Controller
public class YrMaterialController extends GenericController<YrMaterialForm, YrMaterial>{
	
	@Autowired
	private YrMaterialService selfService;

	// 保存实体
	@RequestMapping("/save")
	@ResponseBody
	public HTTPResult<YrMaterialForm> save(String data) throws SwallowException, MyException, InstantiationException,
			IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException {
		HTTPResult<YrMaterialForm> result = new HTTPResult<YrMaterialForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrMaterialForm form = tool.mapper("material", YrMaterialForm.class);
		if(null==form){
			throwExp("param.error");
		}
		if (null == form.getMaterialId() || form.getMaterialId().isEmpty()) {
			form.setMaterialId(UUIDGenerator.getUUID());
			form.setCreateTime(new Date());
		}
		form.setUpdateTime(new Date());
		if (null != form.getImgBase64() && !form.getImgBase64().isEmpty()) {
			YrMaterial entity = selfService.findOne(form.getMaterialId());
			if (entity != null) {
				FileUtil.deleteFile(String.valueOf(PropertyConfigurer.getContextProperty("yiren.img.save.url"))+entity.getIconUrl());
			}
			form.setIconUrl(FileUtil.saveBase64(form.getImgBase64(),
					String.valueOf(PropertyConfigurer.getContextProperty("yiren.img.save.url"))));
		}

		YrMaterial entity = ObjectUtils.objectToObject(form, YrMaterial.class);
		selfService.save(entity);

		return result;
	}

	/** 删除一条记录 */
	@RequestMapping("delete")
	@ResponseBody
	public HTTPResult<YrMaterialForm> delete(String id) throws MyException {
		HTTPResult<YrMaterialForm> result = new HTTPResult<YrMaterialForm>();
		selfService.delete(id);
		return result;
	}

	/**
	 * 批量删除
	 * 
	 * @param data
	 * @return
	 * @throws SwallowException
	 * @throws MyException
	 */
	@RequestMapping("deleteBatch")
	@ResponseBody
	public HTTPResult<YrMaterialForm> deleteBatch(String data) throws SwallowException, MyException {
		HTTPResult<YrMaterialForm> result = new HTTPResult<YrMaterialForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		String[] ids = tool.mapper("ids", String[].class);
		for (String id : ids) {
			selfService.delete(id);
		}
		return result;
	}

	/** 获取所有记录 */
	@RequestMapping("findAll")
	@ResponseBody
	public HTTPResult<YrMaterialForm> findAll() throws MyException, InstantiationException, IllegalAccessException,
			IllegalArgumentException, InvocationTargetException {
		HTTPResult<YrMaterialForm> result = new HTTPResult<YrMaterialForm>();
		List<YrMaterial> entityList = selfService.findAll();
		List<YrMaterialForm> formList = new ArrayList<YrMaterialForm>();
		for (YrMaterial entity : entityList) {
			YrMaterialForm form = ObjectUtils.objectToObject(entity, YrMaterialForm.class);
			//设置图片路径
//			if(!StringUtil.isEmpty(form.getIconUrl())){
//				form.setIconUrl(String.valueOf(PropertyConfigurer.getContextProperty("yiren.img.save.url"))+form.getIconUrl());
//			}
			formList.add(form);
		}
		result.setData(formList);
		return result;
	}

	@Override
	public YrMaterialForm toForm(YrMaterial e) throws Exception {
		YrMaterialForm form = ObjectUtils.objectToObject(e, YrMaterialForm.class);
		return form;
	}

	@Override
	public YrMaterial toEntity(YrMaterialForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
